package com.nl.lib_project

import android.app.Activity
import android.os.Debug
import com.github.gzuliyujiang.dialog.DialogConfig
import com.github.gzuliyujiang.dialog.DialogStyle
import com.google.gson.Gson
import com.nl.lib_core.BaseApplication
import com.nl.lib_core.tool.ReadLocalDatasUtils
import com.nl.lib_core.tool.SPUtils
import com.nl.lib_core.tool.UniqueIDUtils
import com.nl.lib_core.tool.extends.getColorResources
import com.nl.lib_project.datas.LocalDbAutism
import com.nl.lib_project.entity.dbtype.AnswerResultType
import com.nl.lib_project.net.EasyNetProxy
import com.tencent.mmkv.MMKV
import me.jessyan.autosize.AutoSize
import me.jessyan.autosize.AutoSizeConfig
import me.jessyan.autosize.onAdaptListener
import me.jessyan.autosize.utils.AutoSizeLog
import me.jessyan.autosize.utils.ScreenUtils

/** 作者: wangfei 创建时间: 2021/9/6 包名: com.nl.app 类名: MEvaluationApplication */
open class MEvaluationApplication : BaseApplication() {

  companion object {
    var DegreeHashMap: LinkedHashMap<String, String>? = null
    var GenderList: List<String>? = null
    var MarriageList: List<String>? = null
    var QuestionResultInfo: HashMap<String, List<String>>? = null
  }

  override fun onCreate() {
    Debug.startMethodTracing("app_stace")
    super.onCreate()
    initConfig()
    Debug.stopMethodTracing()
  }

  override fun onTerminate() {
    super.onTerminate()
    UniqueIDUtils.clearUniqueFile(instance!!.baseContext)
  }

  private fun initConfig() {
    // TODO 后面把这些操作放入当前专门的app管理类处理
    SPUtils.FILE_NAME = "nl_Medicale_Evaluation"
    // 设置默认值是“Personal”
    SPUtils.put(
        instance!!.baseContext,
        LocalDbAutism.SP_KEY_USER_TYPE,
        LocalDbAutism.SP_VALUE_USER_TYPE_PERSONAL)
    DialogConfig.setDialogStyle(DialogStyle.Three)
    DialogConfig.getDialogColor().apply {
      okTextColor(baseContext.getColorResources(com.nl.lib_core.R.color.project_main_color))
      cancelTextColor(baseContext.getColorResources(com.nl.lib_core.R.color.project_main_color))
      titleTextColor(baseContext.getColorResources(com.nl.lib_core.R.color.colorPrimaryDark))
    }
    getResultType()
    MMKV.initialize(instance!!.baseContext)
    SPUtils.put(
        instance!!.baseContext,
        LocalDbAutism.SP_KEY_UUID,
        UniqueIDUtils.getUniqueID(instance!!.baseContext))
    EasyNetProxy.getInit()
    // 当 App 中出现多进程, 并且您需要适配所有的进程, 就需要在 App 初始化时调用 initCompatMultiProcess()
    AutoSize.initCompatMultiProcess(this)
    AutoSizeLog.setDebug(BuildConfig.DEBUG)
    AutoSizeConfig.getInstance().onAdaptListener =
        object : onAdaptListener {
          override fun onAdaptBefore(target: Any?, activity: Activity?) {
            AutoSizeConfig.getInstance().screenWidth = ScreenUtils.getScreenSize(activity)[0]
            AutoSizeConfig.getInstance().screenHeight = ScreenUtils.getScreenSize(activity)[1]
          }

          override fun onAdaptAfter(target: Any?, activity: Activity?) {}
        }
  }

  private fun getResultType() {
    val jsonData = ReadLocalDatasUtils.getJsonFromAssets(this, "ResultType.json")
    val answerResultType = Gson().fromJson(jsonData, AnswerResultType::class.java)
    DegreeHashMap = answerResultType.BasicInfo.Degree
    GenderList = answerResultType.BasicInfo.Gender
    MarriageList = answerResultType.BasicInfo.Marriage
    QuestionResultInfo = answerResultType.QuestionInfo
  }
}
